import configparser
import os

from CommonOperations.project_root_path import project_root_path


def get_all_configs():
    """获取全部配置项，合成字典返回"""
    all_configs = {}
    project_path = project_root_path()
    ini_file = os.path.join(project_path, "tool_setting.ini")
    conf = configparser.ConfigParser()
    conf.read(ini_file)

    all_configs['version'] = "v1.6"
    all_configs['createtime'] = "2022-09-26"

    all_configs['img_w'] = conf.getint("Image", "default_w")
    all_configs['img_h'] = conf.getint("Image", "default_h")

    all_configs['check_list'] = [{"text": "JPG",
                                  "value": conf.getboolean("Image", "default_jpg")},
                                 {"text": "PNG",
                                  "value": conf.getboolean("Image", "default_png")},
                                 {"text": "GIF",
                                  "value": conf.getboolean("Image", "default_gif")}]
    all_configs['big_dir'] = conf.get("Image", "big_dir")
    all_configs['small_dir'] = conf.get("Image", "save_dir")
    all_configs['whether_add_border'] = [{"text": "图片描边",
                                          "value": conf.getboolean("Image", "whether_add_border")}]
    all_configs['border_color'] = conf.get("Image", "border_color")
    all_configs['border_width'] = conf.getint("Image", "border_width")

    all_configs['font_size'] = conf.getint("Font", "default_size")
    all_configs['font_color'] = conf.get("Font", "default_color")
    all_configs['timestamp'] = [{"text": "时间戳",
                                 "value": conf.getboolean("Time", "whether_timestamp")}]
    all_configs['whether_add_text'] = [{"text": "文字水印",
                                        "value": conf.getboolean("Font", "whether_add_text")}]

    return all_configs


def get_local_config():
    """获取本地配置文件内默认数据"""
    config_dict = {}
    project_path = project_root_path()
    ini_file = os.path.join(project_path, "tool_setting.ini")
    conf = configparser.ConfigParser()
    conf.read(ini_file)
    for s in conf.sections():
        single_i = {}
        for i in conf.items(s):
            try:
                a = conf.getint(s, i[0])
            except ValueError:
                try:
                    a = conf.getboolean(s, i[0])
                except ValueError:
                    a = conf.get(s, i[0])
            single_i[i[0]] = a
        config_dict[s+"_setting"] = single_i

    # 去除不需要的字段
    del config_dict['Image_setting']['big_dir']
    del config_dict['Image_setting']['save_dir']

    return config_dict
